package attack.snowman;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.geom.Rectangle;

import framework.AnimationManager;
import framework.BasicFighter;
import framework.C;
import framework.Util;
import attack.nartan.FireBall;

public class IceStrike extends FireBall {
	
	protected int initialX;
	
	protected int distance;
	
	protected static final int SPEED = 2500;

	public IceStrike(BasicFighter fighter) {
		super(fighter);
		AnimationManager n = fighter.getAnimations();
		this.spriteSheet = n.getSpriteSheet();
		Image[] frames = new Image[3];
		frames[0] = this.spriteSheet.getSprite(2, 3);
		frames[1] = this.spriteSheet.getSprite(3, 3);
		frames[2] = this.spriteSheet.getSprite(4, 3);
		this.attackAnimation = new Animation(frames, 100);
		this.attackAnimation.setDuration(2, 200);
		this.attackAnimationBackward = Util.getFlippedAnimation(this.attackAnimation, true, false);
		this.attackAnimation.setLooping(false);
		this.attackAnimationBackward.setLooping(false);
		
		frames = new Image[1];
		frames[0] = this.spriteSheet.getSprite(0, 4);
		this.ball = new Animation(frames, 70);
		this.ballBackward = Util.getFlippedAnimation(this.ball, true, false);

		this.attacker = fighter;
		this.setWidth((int)fighter.getWidth());
		this.setHeight((int)fighter.getHeight());
		this.interruptOnHit = false;
		this.distance = (int) (getWidth() * 1.7);
		
		this.impact = new Animation();
		this.impact.addFrame(spriteSheet.getSprite(1, 4), 350);
		interruptOnHit = false;
		
		try {
			this.hitSound = new Sound(C.ASSETS_FOLDER + "ice.ogg");
			this.parrySound = new Sound(C.ASSETS_FOLDER + "parry.ogg");
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void trigger() {
		super.trigger();
		this.completed = false;
		this.hit = false;
		this.interruptOnBoundHit = false;
		Rectangle hitbox = getAttacker().getHitBox();
		setXspeed(getAttacker().isForward() ? SPEED : -SPEED);
		setY((int)(getAttacker().getY() - getAttacker().getHitBox().getHeight() / 3));
		if (getAttacker().isForward()) {
			this.currentAttackAnimation = this.attackAnimation;
			this.currentBall = this.ball;
			initialX = (int)(hitbox.getX() - hitbox.getWidth() * 8);
			setX(initialX);
		} else {
			this.currentAttackAnimation = this.attackAnimationBackward;
			this.currentBall = this.ballBackward;
			initialX = (int)(hitbox.getX() + hitbox.getWidth());
			setX(initialX);
		}
		this.currentAttackAnimation.restart();
		getAttacker().setYspeed(Math.abs(getAttacker().getYspeed()));
	}
	
	@Override
	public void draw(Graphics g) {
		// notification that the preparing animation has been completed
		completed = this.currentAttackAnimation.isStopped();
		if(isBlocking()) {
			this.currentAttackAnimation.draw(getAttacker().getX(), getAttacker().getY(), getWidth(), getHeight());
		}
		if (completed) {
		    this.currentBall.draw(getX(), getY(), getWidth(), getHeight());
		}
	}
	
	@Override
	public void update(GameContainer gc, int delta) {
		super.update(gc, delta);
		if (completed) {
			//setX(getX() + getXspeed());
		}
		if (Math.abs(initialX - getX()) >= distance * getScale()) {
			interrupt();
		}
	}	
	
	@Override
	public int getDuration() {
		return 50000;
	}

	@Override
	public int getPower() {
		return 12;
	}
	
	@Override
	public Rectangle getHitBox() {
		int offset = getWidth() / 6;
		int width = 4 * offset;
		Rectangle hitBox;
		hitBox = null;
		if (currentAttackAnimation.isStopped()) {
			if (this.currentBall == this.ball) {
				hitBox = new Rectangle((int)(getX() + width / 2), getY() + offset, width, offset);	
			} else {
				hitBox = new Rectangle((int)(getX()), getY() + offset, width, offset);	
			}
		} 
		return hitBox;
	}

	@Override
	public boolean isBlocking() {
		return isActive() && !completed;
	}

	@Override
	public boolean isDrawingAttacker() {
		return isActive() && !completed;
	}

	@Override
	public boolean isInterruptable() {
		return this.isActive() && !this.currentAttackAnimation.isStopped();
	}	
	
	
	
}
